package com.huike.viewpage;

import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import java.util.ArrayList;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

/**
 * viewPage 使用demo (可以通过手势左右切换页面)
 */
public class MainActivity extends AppCompatActivity {

    private ViewPager mVpVp;
    private RadioGroup mRgMain;
    private RadioButton mRbSelect1;
    private RadioButton mRbSelect2;
    private RadioButton mRbSelect3;
    private ArrayList<MyFrangmen> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mVpVp = (ViewPager) findViewById(R.id.vp_vp);
        mRgMain = (RadioGroup) findViewById(R.id.rg_main);
        mRbSelect1 = (RadioButton) findViewById(R.id.rb_select1);
        mRbSelect2 = (RadioButton) findViewById(R.id.rb_select2);
        mRbSelect3 = (RadioButton) findViewById(R.id.rb_select3);
        mRbSelect1.setChecked(true);

        //创建数据 添加布局
        list = new ArrayList<>();
        list.add(new MyFrangmen("页面一"));
        list.add(new MyFrangmen("页面二sss"));
        list.add(new MyFrangmen("页面三"));

        //设置适配器
        mVpVp.setAdapter(new adapterPager(getSupportFragmentManager()));

        //小圆点被点击/改变
        mRgMain.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            //小圆点 显示状态与viewPage绑定
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i){
                    case R.id.rb_select1:
                        mVpVp.setCurrentItem(0);//setCurrentItem 设置viewPage item 显示位置
                        break;
                    case R.id.rb_select2:
                        mVpVp.setCurrentItem(1);
                        break;
                    case R.id.rb_select3:
                        mVpVp.setCurrentItem(2);
                        break;
                }
            }
        });

        //监听viewPage 页面切换
        mVpVp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if(position == 0){
                    mRbSelect1.setChecked(true);

                }else if(position == 1){
                    mRbSelect2.setChecked(true);

                }else if(position == 2){
                    mRbSelect3.setChecked(true);

                }

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    class adapterPager extends FragmentPagerAdapter {


        public adapterPager(@NonNull FragmentManager fm) {
            super(fm);
        }

        @NonNull
        @Override
        public Fragment getItem(int position) {
            return list.get(position);
        }

        @Override
        public int getCount() {
            return list.size();
        }
    }
}
